package com.ruoyi.system.mapper;

import java.time.LocalDateTime;
import java.util.List;
import com.ruoyi.system.domain.NdDevicesWorkStatus;
import com.ruoyi.system.domain.NdDevicesAttributeLink;
import com.ruoyi.system.domain.Nd_DevicesWrokStatus;
import org.apache.ibatis.annotations.*;

/**
 * 防溜设备实时状态Mapper接口
 * 
 * @author ruoyi
 * @date 2024-07-05
 */
public interface NdDevicesWorkStatusMapper 
{
    /**
     * 查询防溜设备实时状态
     * 
     * @param deviceId 防溜设备实时状态主键
     * @return 防溜设备实时状态
     */
    public NdDevicesWorkStatus selectNdDevicesWorkStatusByDeviceId(Long deviceId);


    public List<NdDevicesAttributeLink> selectNdDevicesAttributeByDeviceId(Long deviceId);


    @Select("select * from nd_devices_work_status WHERE nd_devices_work_status.Device_Id = #{deviceId}")
    public Nd_DevicesWrokStatus selectByDeviceId(Long deviceId);
    /**
     * 查询防溜设备实时状态列表
     * 
     * @param ndDevicesWorkStatus 防溜设备实时状态
     * @return 防溜设备实时状态集合
     */
    public List<NdDevicesWorkStatus> selectNdDevicesWorkStatusList(NdDevicesWorkStatus ndDevicesWorkStatus);

    /**
     * 新增防溜设备实时状态
     * 
     * @param ndDevicesWorkStatus 防溜设备实时状态
     * @return 结果
     */
    public int insertNdDevicesWorkStatus(NdDevicesWorkStatus ndDevicesWorkStatus);

    /**
     * 修改防溜设备实时状态
     * 
     * @param ndDevicesWorkStatus 防溜设备实时状态
     * @return 结果
     */
    public int updateNdDevicesWorkStatus(NdDevicesWorkStatus ndDevicesWorkStatus);

    /**
     * 删除防溜设备实时状态
     * 
     * @param deviceId 防溜设备实时状态主键
     * @return 结果
     */
    public int deleteNdDevicesWorkStatusByDeviceId(Long deviceId);

    /**
     * 批量删除防溜设备实时状态
     * 
     * @param deviceIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteNdDevicesWorkStatusByDeviceIds(Long[] deviceIds);

    /**
     * 批量删除设备属性信息
     * 
     * @param deviceIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteNdDevicesAttributeLinkByDeviceIds(Long[] deviceIds);
    
    /**
     * 批量新增设备属性信息
     * 
     * @param ndDevicesAttributeLinkList 设备属性信息列表
     * @return 结果
     */
    public int batchNdDevicesAttributeLink(List<NdDevicesAttributeLink> ndDevicesAttributeLinkList);
    

    /**
     * 通过防溜设备实时状态主键删除设备属性信息信息
     * 
     * @param deviceId 防溜设备实时状态ID
     * @return 结果
     */
    public int deleteNdDevicesAttributeLinkByDeviceId(Long deviceId);

//    @Select("select Device_Id from nd_devices_work_status as ndws where ndws.Device_Type = #{type} and ndws.OpenCloseBox = #{openStatus} " +
//            "and ndws.update_time < #{updateTime}")
//    List<Integer> getByTypeAndStatus(Integer type, Integer openStatus, LocalDateTime updateTime);

    @Select("select Device_Id from nd_devices_work_status as ndws where \tEXISTS (\n" +
            "SELECT\n" +
            "\t1 \n" +
            "FROM\n" +
            "\tnd_devices_attribute_link AS ndal \n" +
            "WHERE\n" +
            "\tndws.Device_Id = ndal.Device_Id \n" +
            "\tAND ndal.Attribute_Id = 6 \n" +
            "\tAND ndal.Attribute_Value = 0 \n" +
            "\t) and ndws.Device_Type = #{type} and ndws.OpenCloseBox = #{openStatus} " +
            "and ndws.update_time < #{updateTime}")
    List<Integer> getByTypeAndStatus(@Param("type") Integer type, @Param("openStatus") Integer openStatus, @Param("updateTime") LocalDateTime updateTime);

    @Select("select Device_Id from nd_devices_work_status as ndws where ndws.Station_Id = #{id} " +
            "and (ndws.Device_Type = 2 or ndws.Device_Type = 3) and in_time IS NOT NULL and in_time <= #{in_time}")
    List<Long> selectShoeTimein(@Param("id") String id, @Param("in_time") String in_time);

    @Select("select Device_Id from nd_devices_work_status as ndws where ndws.Station_Id = #{id} " +
            "and (ndws.Device_Type = 2 or ndws.Device_Type = 3) and out_time IS NOT NULL and out_time <= #{out_time}")
    List<Long> selectShoeTimeout(@Param("id") String id,  @Param("out_time") String out_time);

    //通过VIN跟站id查询设备id
    @Select("select Device_Id from nd_devices_work_status as ndws where ndws.Device_VIN = #{deviceVIN} and Station_Id = #{stationId}")
    Integer selectDeviceIdByStationIdAndDeviceVIN(@Param("stationId") int stationId,@Param("deviceVIN") String deviceVIN);
    /**
     * 插入设备工作状态记录
     * @param devId 设备 ID
     * @param defaultBoxId 默认盒子 ID
     * @return 插入成功的记录数
     */
    @Insert("INSERT INTO nd_devices_work_status (" +
            "Device_Id, " +
            "Device_Code, " +
            "Device_VIN, " +
            "Device_Type, " +
            "Track_Id, " +
            "Railway_CarNo, " +
            "SortNo, " +
            "Postion, " +
            "Person_Id, " +
            "qbid, " +
            "Work_Type_Id, " +
            "Operation_Type_Id, " +
            "Vbat_Value, " +
            "InOutBox, " +
            "OpenCloseBox, " +
            "IsFree, " +
            "Inbox_Id, " +
            "Station_Id, " +
            "zmlm, " +
            "DevTemperature, " +
            "IsOnline, " +
            "VbatPercent, " +
            "Field_Id, " +
            "placement_time, " +
            "default_box_id " +
            ") SELECT " +
            "nd_devices.Device_Id, " +
            "nd_devices.Device_Code, " +
            "nd_devices.Device_VIN, " +
            "nd_devices.Device_Type, " +
            "0 AS Track_Id, " +
            "'' AS Railway_CarNo, " +
            "0 AS SortNo, " +
            "0 AS Postion, " +
            "0 AS Person_Id, " +
            "NULL AS qbid, " +
            "2 AS Work_Type_Id, " +
            "1 AS Operation_Type_Id, " +
            "4.01 AS Vbat_Value, " +
            "0 AS InOutBox, " +
            "2 AS OpenCloseBox, " +
            "0 AS IsFree, " +
            "#{defaultBoxId} AS Inbox_Id, " +
            "nd_devices.Station_Id, " +
            "sys_dept.ZMLM, " +
            "20 AS DevTemperature, " +
            "1 AS IsOnline, " +
            "100 AS VbatPercent, " +
            "nd_devices.Field_Id, " +
            "NULL AS placement_time, " +
            "#{defaultBoxId} " +
            "FROM " +
            "nd_devices " +
            "INNER JOIN sys_dept ON nd_devices.Station_Id = sys_dept.dept_id " +
            "AND nd_devices.Device_Id = #{devId}")
    int insertIntoDeviceWorkStatus(@Param("devId") Long devId, @Param("defaultBoxId") Integer defaultBoxId);


    @Update("UPDATE nd_devices_work_status ndws " +
            "JOIN nd_devices nd ON ndws.Device_Id = nd.Device_Id " +
            "JOIN sys_dept sd ON nd.Station_Id = sd.dept_id " +
            "SET " +
            "ndws.Device_Code = nd.Device_Code, " +
            "ndws.Device_VIN = nd.Device_VIN, " +
            "ndws.Device_Type = nd.Device_Type, " +
            "ndws.Track_Id = 0, " +
            "ndws.Railway_CarNo = '', " +
            "ndws.SortNo = 0, " +
            "ndws.Postion = 0, " +
            "ndws.Person_Id = 0, " +
            "ndws.qbid = NULL, " +
            "ndws.Work_Type_Id = 2, " +
            "ndws.Operation_Type_Id = 1, " +
            "ndws.Vbat_Value = 4.01, " +
            "ndws.InOutBox = 0, " +
            "ndws.OpenCloseBox = 2, " +
            "ndws.IsFree = 0, " +
//            "ndws.Inbox_Id = nd.Inbox_Id, " +
            "ndws.Station_Id = nd.Station_Id, " +
            "ndws.zmlm = sd.ZMLM, " +
            "ndws.DevTemperature = 20, " +
            "ndws.IsOnline = 1, " +
            "ndws.VbatPercent = 100, " +
            "ndws.Field_Id = nd.Field_Id, " +
            "ndws.placement_time = NULL, " +
            "ndws.default_box_id = nd.default_box_id " +
            "WHERE " +
            "nd.Device_Id = #{devId}")
    int updateDeviceWorkStatus(@Param("devId") Long devId);


    @Delete("DELETE FROM nd_devices_work_status WHERE Device_Id = #{devId}")
    int deleteDevice(@Param("devId") Long devId);





}
